<?php
/**
 * 奖品的添加与修改
 * User: Administrator
 * Date: 2019\2\13 0013
 * Time: 18:24
 */

namespace app\Lib\Model\Present;
use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\Img\Img;
use app\Lib\InterfaceIn\Present\OperationPresent as Api;
use app\Lib\InterfaceIn\Present\Present;
use app\Lib\InterfaceIn\User\UserHandle;

class OperationPresent implements  Api
{
    private $probabilityNum;
    private $presentType;
    private $presentName;
    /** @var Img $presentImgId */
    private $presentImgId;
    private $presentNum;
    /** @var UserHandle $opUser */
    private $opUser;
    /** @var Present $present */
    private $present;
    private $table;
    public function setProbabilityNum($probabilityNum)
    {
        $this->probabilityNum = $probabilityNum;
        return $this;
    }

    public function setPresentType($presentType)
    {
        $this->presentType = $presentType;
        return $this;
    }

    public function setPresentName($presentName)
    {
        $this->presentName = $presentName;
        return $this;
    }

    public function setPresentImgId(Img $presentImgId)
    {
        $this->presentImgId = $presentImgId;
        return $this;
    }

    public function setPresentNum($presentNum)
    {
        $this->presentNum = $presentNum;
        return $this;
    }

    public function setOpUser(UserHandle $opUser)
    {
        $this->opUser = $opUser;
        return $this;
    }

    public function setPresent(Present $present)
    {
        $this->present = $present;
        return $this;
    }

    /**
     * @return bool
     * @throws BlException
     */
    public function run()
    {
        $table = $this->getTable();
        $data = [
            $table->_present_img_id=>$this->presentImgId->getImgId(),
            $table->_present_name=>$this->presentName,
            $table->_present_num=>$this->presentNum,
            $table->_present_type=>$this->presentType,
            $table->_probability_num=>$this->probabilityNum,
        ];
        if($this->present){
            $this->update($data);
        }else{
            $this->add($data);
        }
        return true;
    }

    /**
     * @param $data
     * @throws BlException
     */
    private function add($data){
        $table = $this->getTable();
        $data[$table->_create_time] = time();
        $data[$table->_create_uid] = $this->opUser->getUid();
        $result = $table->insert($data);
        if(!$result){
            throw new BlException('添加失败');
        }
    }

    /**
     * @param $data
     * @throws BlException
     */
    private function update($data){
        $table = $this->getTable();
        $data[$table->_update_time] = time();
        $data[$table->_update_uid] = $this->opUser->getUid();
        $result = $table->update($data);
        if(!$result){
            throw new BlException('修改失败');
        }
    }

    private function check(){
        

    }

    /**
     * 数据库调用
     * @return \app\Lib\Model\DbModel\Present
     */
    private function getTable(){
        if(empty($this->table)){
            $this->table = new \app\Lib\Model\DbModel\Present();
        }
        return $this->table;
    }
}